﻿using System.Xml.Linq;
using LINQPad.Extensibility.DataContext;

namespace MongoLP
{
    /// <summary>
    /// Much thanks to http://normlinqpaddriver.codeplex.com/ from which
    /// a great deal of this code is derived.
    /// 
    /// There is method to the madness.  This driver uses the standard MongoDB C#
    /// driver from 10gen (+ FluentMongo, and for good measure MongoRepository).
    /// NoRM is no longer actively developed.
    /// </summary>
    public class Properties
    {
        readonly XElement _driverData;

        /// <summary>
        /// Initializes a new instance of the <see cref="Properties"/> class.
        /// </summary>
        /// <param name="cxInfo">The cx info.</param>
        public Properties(IConnectionInfo cxInfo)
        {
            ConnectionInfo = cxInfo;
            _driverData = cxInfo.DriverData;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Properties"/> is persist.
        /// </summary>
        /// <value>
        ///   <c>true</c> if persist; otherwise, <c>false</c>.
        /// </value>
        public bool Persist
        {
            get { return ConnectionInfo.Persist; }
            set { ConnectionInfo.Persist = value; }
        }

        /// <summary>
        /// ConnectionString
        /// </summary>
        /// <value>
        /// The connection string.
        /// </value>
        public string ConnectionString
        {
            get { return (string)_driverData.Element("ConnectionString") ?? string.Empty; }
            set { _driverData.SetElementValue("ConnectionString", value); }
        }

        /// <summary>
        /// Path to the assembly containing the actual entities
        /// </summary>
        /// <value>
        /// The entity assembly path.
        /// </value>
        public string EntityAssemblyPath
        {
            get { return (string)_driverData.Element("EntityAssemblyPath") ?? string.Empty; }
            set { _driverData.SetElementValue("EntityAssemblyPath", value); }
        }

        /// <summary>
        /// Full name of the type used to identify entities.
        /// </summary>
        /// <value>
        /// The entity interface.
        /// </value>
        public string EntityInterface
        {
            get { return (string)_driverData.Element("EntityInterface") ?? string.Empty; }
            set { _driverData.SetElementValue("EntityInterface", value); }
        }

        /// <summary>
        /// Gets the connection information.
        /// </summary>
        /// <value>The connection information object.</value>
        public IConnectionInfo ConnectionInfo { get; private set; }
    }
}
